<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Animal extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        // Carrega model
        $this->load->model('admin/animal_model');

        $data = array(
            'title_for_layout' => 'Administra&ccedil;&atilde;o | Animal',
            'title_for_page'   => 'Listagem de Animal',
            'animais'          => $this->animal_model->listar(),
            'content_layout'   => 'admin/modulos/animal/animal'
        );
        $this->parser->parse('admin/layout_main',$data);
    }

    public function cadastro()
    {
        $data = array(
            'title_for_layout' => 'Administra&ccedil;&atilde;o | Animal',
            'title_for_page'   => 'Cadastro de Animais',
            'content_layout'   => 'admin/modulos/animal/cadastrar'
        );
        $this->parser->parse('admin/layout_main',$data);
    }

    public function adicionar()
    {
        $config = array(
            array(
                'field' => 'pai',
                'label' => 'Pai',
                'rules' => 'required|min_length[4]|max_length[20]'
            ),
            array(
                'field' => 'mae',
                'label' => 'Mae',
                'rules' => 'required|min_length[4]|max_length[20]'
            )
        );
        $this->form_validation->set_rules( $config );
        if ($this->form_validation->run() == FALSE)
        {
            $this->index();
        }
        else
        {
            // Pega os dados
            $data['pai'] = $this->input->post( 'pai' );
            $data['mae'] = $this->input->post( 'mae' );
            
            $this->load->model('admin/filiacao_model');
            if( $this->filiacao_model->cadastrar( $data ) )
                redirect( base_url(). 'admin/filiacao/', 'refresh' );
            else
              echo 'Erro ao inserir a filiacao';
        }
    }

    public function grava_alteracao()
    {
      $config = array(
            array(
                'field' => 'pai',
                'label' => 'Pai',
                'rules' => 'required|min_length[4]|max_length[20]'
            ),
            array(
                'field' => 'mae',
                'label' => 'Mae',
                'rules' => 'required|min_length[4]|max_length[20]'
            )
        );
        $this->form_validation->set_rules($config);

        if ( $this->form_validation->run() == FALSE )
        {
            $this->alterar( $this->input->post( 'codfiliacao' ) );
        }
        else
        {
            // Pega os dados
            $data['codnoticia'] = $this->input->post( 'codnoticia' );
            $data['pai']        = $this->input->post( 'pai' );
            $data['mae']        = $this->input->post( 'mae' );

            $this->load->model('admin/filiacao_model');

            if ( $this->filiacao_model->grava_alteracao($data))
                redirect(base_url(). 'admin/filiacao/', 'refresh');
            else
              echo 'Erro ao inserir a filiacao';
        }
    }
    
    public function excluir($id)
    {
        $this->load->model('admin/filiacao_model');
        if ($this->filiacao_model->excluir($id))
        {
            redirect('admin/filiacao/', 'refresh');
        }
        else
        {
            echo "Erro ao excluir filiacao";
        }
    }
}
